<?php
session_start();
include("load-settings.php");

if(!isset($_SESSION['user']))
	header("Location: login.php");

require_once("unirest/lib/Unirest.php");

$user = $_SESSION['user'];

$result = mysql_query("SELECT * FROM user WHERE id = $user");
$row = mysql_fetch_array($result);

if($row['type'] == 0)
	header("Location: home.php");

$type = $row['type'];
$level = $row['level'];
$username = $row['username'];

function numberToRoman($num) 
 {
     // Make sure that we only use the integer portion of the value
     $n = intval($num);
     $result = '';
 
     // Declare a lookup array that we will use to traverse the number:
     $lookup = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
     'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
     'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
 
     foreach ($lookup as $roman => $value) 
     {
         // Determine the number of matches
         $matches = intval($n / $value);
 
         // Store that many characters
         $result .= str_repeat($roman, $matches);
 
         // Substract that from the number
         $n = $n % $value;
     }
 
     // The Roman numeral should be built, return it
     return $result;
 }
 function romanToNumber($roman)
 {
	 $romans = array(
    'M' => 1000,
    'CM' => 900,
    'D' => 500,
    'CD' => 400,
    'C' => 100,
    'XC' => 90,
    'L' => 50,
    'XL' => 40,
    'X' => 10,
    'IX' => 9,
    'V' => 5,
    'IV' => 4,
    'I' => 1,
);

$result = 0;

foreach ($romans as $key => $value) {
    while (strpos($roman, $key) === 0) {
        $result += $value;
        $roman = substr($roman, strlen($key));
    }
}
return $result;
}
?>
<!doctype html>
<html>
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, maximum-scale=1, initial-scale=1, user-scalable=0">

  <!-- Always force latest IE rendering engine or request Chrome Frame -->
  <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <link rel="icon" href="http://www.veteranboosters.com/wp-content/uploads/2014/04/favicon4.png" type="image/x-icon"/>
    <link rel="shortcut icon" href="http://www.veteranboosters.com/wp-content/uploads/2014/04/favicon4.png" type="image/x-icon"/>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:400,600,800">

  <!-- Use title if it's in the page YAML frontmatter -->
  <title>Veteran Boosters Order Tracking</title>

  <!--[if lt IE 9]>
  <script src="../../javascripts/vendor/html5shiv.js" type="text/javascript"></script>
  <script src="../../javascripts/vendor/excanvas.js" type="text/javascript"></script>
  <![endif]-->
<link rel="stylesheet" href="css/bootstrap.min.css" />
<link rel="stylesheet" href="css/font-awesome.css" />
<link rel="stylesheet" href="css/fullcalendar.css" />
<link rel="stylesheet" href="css/jquery.jscrollpane.css" />	
<link rel="stylesheet" href="css/unicorn.css" />
<link href="stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
  <script src="javascripts/application.js" type="text/javascript"></script>
</head>

<?php
	include("top.php");
?>

  <div class="container-fluid padded">
    <div class="row" style="padding-left: 15px;">

      <!-- Breadcrumb line -->
			<div id="breadcrumb" >
				<a href="/" title="" class="tip-bottom" data-original-title="Go to Home"><i class="fa fa-home"></i> Home</a>
				<a href="#">Members</a>
				<a href="#" class="current">Boosters Overview    </a>
			</div>
    </div>
  </div>
  <div class="container-fluid padded">
    <div class="row-fluid">

	<div class="alert alert-info"> 
   <?php echo stripslashes($booster_warning); ?>
</div>
<div class="alert alert-block alert-warning" style="">
   <?php echo stripslashes($skype_info); ?>
</div>
  </div>
  <style type="text/css">
   table td img.tier { width: 50px; height: 50px; }
</style>
<div class="row">
   <div class="col-lg-12">
      <div class="box">
	  <div class="box-header">
            <span class="title">Boosting Orders</span>
         </div>
         <div class="box-content">
            
               <table class = "table table-bordered table-striped table-hover" cellpadding="0" cellspacing="0" border="0">
                  <thead>
                     <tr ><th rowspan="1" colspan="1" style="width: 58px;"><div>Order ID</div></th>
					 <th rowspan="1" colspan="1" style="width: 99px;"><div>Purchase Date</div></th>
					 <th rowspan="1" colspan="1" style="width: 124px;"><div>Server</div></th>
					 <th rowspan="1" colspan="1"><div>Current Placement</div></th>
					 <th rowspan="1" colspan="1"><div>Desired</div></th>
					 <th rowspan="1" colspan="1"><div>Payment</div></th>
					 <th width="20%" rowspan="1" colspan="1" style="width: 186px;"><div>Progress</div></th>
					 <th rowspan="1" colspan="1" style="width: 100px;"><div>Claimed By</div></th>
					 <th rowspan="1" colspan="1" style="width: 160px"><div>Actions</div></th></tr>
                  </thead>
                  
               <tbody id = "boosting_inner">
			   <?php
					

					$result = mysql_query("SELECT * FROM order_record WHERE complete = 0");

					$unclamedArray = array();
					$claimedArray = array();

					$z = 0;
					$a = 0;

					$html_buffer = '';

					while($row = mysql_fetch_array($result))
					{
						if($row['end_tier'] != null && $row['end_tier'] != "")
						{
							/*if($row['end_tier'] == "Bronze" || $row['end_tier'] == "Silver" || $row['end_tier'] == "Gold")
								$status_req = 1;
							else if($row['end_tier'] == "Platinum" || $row['end_tier'] == "Diamond")
								$status_req = 2;
							else if($row['end_tier'] == "Challenger")
								$status_req = 3;*/

							
							if($row['end_tier'] == "Bronze" || $row['end_tier'] == "Silver")
							{
								$status_req = 1;
							}
							else if($row['end_tier'] == "Gold")
							{
								if($row['end_division'] == 5)
								{
									$status_req = 1;
								}
								if($row['end_division'] == 4)
								{
									$status_req = 2;
								}
								if($row['end_division'] == 3)
								{
									$status_req = 3;
								}
								if($row['end_division'] == 2)
								{
									$status_req = 4;
								}
								if($row['end_division'] == 1)
								{
									$status_req = 5;
								}
							}
							else if($row['end_tier'] == "Platinum")
							{
								if($row['end_division'] == 5)
								{
									$status_req = 6;
								}
								if($row['end_division'] == 4)
								{
									$status_req = 7;
								}
								if($row['end_division'] == 3)
								{
									$status_req = 8;
								}
								if($row['end_division'] == 2)
								{
									$status_req = 9;
								}
								if($row['end_division'] == 1)
								{
									$status_req = 10;
								}
							}
							else if($row['end_tier'] == "Diamond")
							{
								if($row['end_division'] == 5)
								{
									$status_req = 11;
								}
								if($row['end_division'] == 4)
								{
									$status_req = 12;
								}
								if($row['end_division'] == 3)
								{
									$status_req = 13;
								}
								if($row['end_division'] == 2)
								{
									$status_req = 14;
								}
								if($row['end_division'] == 1)
								{
									$status_req = 15;
								}
							}
							else if($row['end_tier'] == "Challenger")
							{
								$status_req = 16;
							}
							
						}
						else
						{
							/*if($row['start_tier'] == "Bronze" || $row['start_tier'] == "Silver" || $row['start_tier'] == "Gold")
								$status_req = 1;
							else if($row['start_tier'] == "Platinum" || $row['start_tier'] == "Diamond" || $row['start_tier'] == "Unranked")
								$status_req = 2;
							else if($row['start_tier'] == "Challenger")
								$status_req = 3;*/

							
							if($row['start_tier'] == "Bronze" || $row['start_tier'] == "Silver")
							{
								$status_req = 1;
							}
							else if($row['start_tier'] == "Gold")
							{
								if($row['start_division'] == 5)
								{
									$status_req = 1;
								}
								if($row['start_division'] == 4)
								{
									$status_req = 2;
								}
								if($row['start_division'] == 3)
								{
									$status_req = 3;
								}
								if($row['start_division'] == 2)
								{
									$status_req = 4;
								}
								if($row['start_division'] == 1)
								{
									$status_req = 5;
								}
							}
							else if($row['start_tier'] == "Platinum")
							{
								if($row['start_division'] == 5)
								{
									$status_req = 6;
								}
								if($row['start_division'] == 4)
								{
									$status_req = 7;
								}
								if($row['start_division'] == 3)
								{
									$status_req = 8;
								}
								if($row['start_division'] == 2)
								{
									$status_req = 9;
								}
								if($row['start_division'] == 1)
								{
									$status_req = 10;
								}
							}
							else if($row['start_tier'] == "Diamond")
							{
								if($row['start_division'] == 5)
								{
									$status_req = 11;
								}
								if($row['start_division'] == 4)
								{
									$status_req = 12;
								}
								if($row['start_division'] == 3)
								{
									$status_req = 13;
								}
								if($row['start_division'] == 2)
								{
									$status_req = 14;
								}
								if($row['start_division'] == 1)
								{
									$status_req = 15;
								}
							}
							else if($row['start_tier'] == "Challenger")
							{
								$status_req = 16;
							}
							

							if($row['previous_tier'] != null && $row['previous_tier'] != "")
							{
								/*if($row['previous_tier'] == "UNRANKED" || $row['previous_tier'] == "BRONZE" || $row['previous_tier'] == "SILVER" || $row['previous_tier'] == "GOLD")
									$status_req = 1;
								else if($row['previous_tier'] == "PLATINUM" || $row['start_tier'] == "DIAMOND" || $row['previous_tier'] == "UNRANKED")
									$status_req = 2;
								else if($row['previous_tier'] == "CHALLENGER")
									$status_req = 3;*/

								
							if($row['previous_tier'] == "UNRANKED" || $row['previous_tier'] == "BRONZE" || $row['previous_tier'] == "SILVER" || $row['previous_tier'] == "GOLD")
							{
								$status_req = 1;
							}
							else if($row['previous_tier'] == "PLATINUM")
							{
								$status_req = 6;
							}
							else if($row['start_tier'] == "DIAMOND")
							{
								$status_req = 11;
							}
							else if($row['start_tier'] == "CHALLENGER")
							{
								$status_req = 16;
							}
							
							}
						}
						
							$html_buffer .= '<tr class=""><td class="center ">'.$row['id'].'</td>';
							$html_buffer .= '<td style="text-align: center;" class=" "><small>'.date("d M Y", $row['timestamp']).'</small>';
							
							$subresult = mysql_query("SELECT * FROM user WHERE id = ".$row['user']);
							$subrow = mysql_fetch_array($subresult);

							if($row['region'] == "na")
								$region = "North America";
							else if($row['region'] == "br")
								$region = "Brazil";
							else if($row['region'] == "lan")
								$region = "Latin America North";
							else if($row['region'] == "euw")
								$region = "Europe West";
							else if($row['region'] == "eun")
								$region = "Europe East";
							else if($row['region'] == "ru")
								$region = "Russia";
							else if($row['region'] == "tr")
								$region = "Turkey";
							else
								$region = $row['region'];

							$html_buffer .= '</td><td class=" ">'.$region.'<br><span class="label label-success">online</span></td>';

							require_once("unirest/lib/Unirest.php");

							$region = $row['region'];
							$summoner = str_replace(" ", "", $row['summoner']);

							
							/*$newresponse = Unirest::get(
							  "https://teemojson.p.mashape.com/player/".$region."/".str_replace(" ", "", $summoner)."/leagues",
							  array(
								"X-Mashape-Authorization" => "XcM4wvOlSewxmSNGstMYMpkjxvZGc8gx"
							  ),
							  null
							);
							$decode = json_decode($newresponse->raw_body)->data->summonerLeagues;

							$lp = 0;
							//echo "<pre>"; var_dump($newresponse); echo "</pre>"; die;
							$x = 0;
							while(isset($decode->array[$x]))
							{
							//$newdecode = json_decode($newresponse->raw_body)->data->summonerLeagues->array[0]->entries->array;
							$newdecode = $decode->array[$x]->entries->array;
							//echo "<pre>"; var_dump($newdecode); echo "</pre>"; die;

							$y = 0;
							while(isset($newdecode[$y]))
							{
								if($newdecode[$y]->playerOrTeamName == $summoner)
								{
									$lp = $newdecode[$y]->leaguePoints;
									break;
								}

								$y++;
							}
							$x++;
							}*/
							$tier = "";
							$rank = "";
							$last_match_result = mysql_query("SELECT * FROM match_history WHERE order_id = ".$row['id']." ORDER BY timestamp DESC LIMIT 1") or die(mysql_error());
							//$tier = $row['start_tier'];
							//$rank = $row['start_division'];
							if(mysql_num_rows($last_match_result) == 0)
							{
								$lp = $row['beginning_lp'];
								$tier = $row['start_tier'];
								$rank = numberToRoman($row['start_division']);
							}
							else
							{
								$last_match = mysql_fetch_array($last_match_result);
								$lp = $last_match['lp_at_end'];
								if($last_match['tier_at_end'] != null && $last_match['tier_at_end'] != "")
								{
									$tier = $last_match['tier_at_end'];
								}
								if($last_match['rank_at_end'] != null && $last_match['rank_at_end'] != "")
								{
									$rank = $last_match['rank_at_end'];
								}

								//$tier = $last_match['tier_at_end'];
								//$rank = $last_match['rank_at_end'];
							}
							if($tier == "")
							{
								/*$response = Unirest::get(
								  "https://teemojson.p.mashape.com/player/$region/$summoner/leagues",
								  array(
									"X-Mashape-Authorization" => "XcM4wvOlSewxmSNGstMYMpkjxvZGc8gx"
								  )
								);

								$decode = json_decode($response->raw_body);

								$x = 0;

								$tier = "UNRANKED";
								$rank = "";

								while(isset($decode->data->summonerLeagues->array[$x]))
								{
									if($decode->data->summonerLeagues->array[$x]->queue == "RANKED_SOLO_5x5")
									{
										$tier = $decode->data->summonerLeagues->array[$x]->tier;
										$rank = $decode->data->summonerLeagues->array[$x]->requestorsRank;
										//$lp = $decode->data->summonerLeagues->array[$x]->leaguePoints;
									}
									$x++;
								}*/

								$lp = $row['beginning_lp'];
								$tier = $row['start_tier'];
								$rank = numberToRoman($row['start_division']);
							}
							$html_buffer .= '<td class="center "><div style = "float: right; ';
							if($row['previous_tier'] != null && $row['previous_tier'] != "")
							{
								$html_buffer .= 'width: 50%;';
							}
							else
							{
								$html_buffer .= 'width: 100%;';
							}
							$html_buffer .= '"><img src="/_FILES/images/'.strtolower($tier).'_'.romanToNumber($rank).'.png" height="50px" width="50px" class="tier"><br>';
							$html_buffer .= '<small><b>'.$tier.' '.$rank.'</b></small><br>';
							$html_buffer .= '<small>LP: '.$lp.'</span></small></div>';
							if($row['previous_tier'] != null && $row['previous_tier'] != "")
							{
								$html_buffer .= '<div style = "float: right; width: 50%;">';
								if($row['previous_tier'] != "UNRANKED" && $row['previous_tier'] != "CHALLENGER")
									$temp_rank = 5;
								else
									$temp_rank = 0;
								$html_buffer .= '<img src="/_FILES/images/'.strtolower($row['previous_tier']).'_'.$temp_rank.'.png" height="50px" width="50px" class="tier"><br><small><span>LAST SEASON</span></small></div>';
							}
							//$html_buffer .= '<td class="center "><img src="/_FILES/eloscript/images/'.strtolower($row['start_tier']).'_'.$row['start_division'].'.png" height="50px" width="50px" class="tier"><br>';
							//$html_buffer .= '<small><b>'.$row['start_tier'].' '.numberToRoman($row['start_division']).'</b></small><br>';

							/*$match_result = mysql_query("SELECT * FROM match_history WHERE order_id = ".$row['id']." ORDER BY timestamp DESC LIMIT 1");
							if(mysql_num_rows($match_result) == 0)
								$html_buffer .= '<small>LP: '.$row['beginning_lp'].'</span></small>';
							else
							{
								$match_row = mysql_fetch_array($match_result);
								$html_buffer .= '<small>LP: '.$match_row['lp_at_end'].'</span></small>';
							}*/

							if($row['end_tier'] != null && $row['end_tier'] != "")
								$html_buffer .= '<td class="center "><img src="/_FILES/images/'.strtolower($row['end_tier']).'_'.$row['end_division'].'.png" height="50px" width="50px" class="tier"><br>'.$row['end_tier'].' '.numberToRoman($row['end_division']).'</small></td>';
							else
							{
								if($row['start_tier'] != "Unranked")
									$html_buffer .= '<td class="center ">'.$row['number_of_wins'].' wins</small></td>';
								else
									$html_buffer .= '<td class="center ">'.$row['number_of_wins'].' games</small></td>';
							}
							$html_buffer .= '<td class="center ">$'.($row['booster_payout']).'</td>';

							$booster_result = mysql_query("SELECT * FROM booster_order WHERE active <> 0 AND order_record = ".$row['id']);
							if(mysql_num_rows($booster_result) != 0)
							{
								$booster_row = mysql_fetch_array($booster_result);

								if($row['end_tier'] != null && $row['end_division'] != '')
								{
									if($row['start_tier'] == "Bronze")
										$start_tier_num = 0;
									if($row['start_tier'] == "Silver")
										$start_tier_num = 5;
									if($row['start_tier'] == "Gold")
										$start_tier_num = 10;
									if($row['start_tier'] == "Platinum")
										$start_tier_num = 15;
									if($row['start_tier'] == "Diamond")
										$start_tier_num = 20;

									if($row['start_division'] == 5)
										$start_tier_num += 0;
									if($row['start_division'] == 4)
										$start_tier_num += 1;
									if($row['start_division'] == 3)
										$start_tier_num += 2;
									if($row['start_division'] == 2)
										$start_tier_num += 3;
									if($row['start_division'] == 1)
										$start_tier_num += 4;

									if($row['end_tier'] == "Bronze")
										$end_tier_num = 0;
									if($row['end_tier'] == "Silver")
										$end_tier_num = 5;
									if($row['end_tier'] == "Gold")
										$end_tier_num = 10;
									if($row['end_tier'] == "Platinum")
										$end_tier_num = 15;
									if($row['end_tier'] == "Diamond")
										$end_tier_num = 20;

									if($row['end_division'] == 5)
										$end_tier_num += 0;
									if($row['end_division'] == 4)
										$end_tier_num += 1;
									if($row['end_division'] == 3)
										$end_tier_num += 2;
									if($row['end_division'] == 2)
										$end_tier_num += 3;
									if($row['end_division'] == 1)
										$end_tier_num += 4;

									$total_steps = $end_tier_num - $start_tier_num;

									/*$response = Unirest::get(
									  "https://teemojson.p.mashape.com/player/".$row['region']."/".str_replace(' ', '', $row['summoner'])."/leagues",
									  array(
										"X-Mashape-Authorization" => "XcM4wvOlSewxmSNGstMYMpkjxvZGc8gx"
									  )
									);

									$decode = json_decode($response->raw_body);

									$x = 0;

									$tier = "UNRANKED";
									$rank = "";

									while(isset($decode->data->summonerLeagues->array[$x]))
									{
										if($decode->data->summonerLeagues->array[$x]->queue == "RANKED_SOLO_5x5")
										{
											$tier = $decode->data->summonerLeagues->array[$x]->tier;
											$rank = $decode->data->summonerLeagues->array[$x]->requestorsRank;
										}
										$x++;
									}

									*/

									$tier = ucwords(strtolower($tier));
									$rank = romanToNumber($rank);

									if($tier == "Unranked")
										$percent = 0;
									else
									{
										if($tier == "Bronze")
											$tier_num = 0;
										if($tier == "Silver")
											$tier_num = 5;
										if($tier == "Gold")
											$tier_num = 10;
										if($tier == "Platinum")
											$tier_num = 15;
										if($tier == "Diamond")
											$tier_num = 20;

										if($rank == 5)
											$tier_num += 0;
										if($rank == 4)
											$tier_num += 1;
										if($rank == 3)
											$tier_num += 2;
										if($rank == 2)
											$tier_num += 3;
										if($rank == 1)
											$tier_num += 4;

										$tier_num -= $start_tier_num;

										$percent = ($tier_num/$total_steps) * 100;
									}
								}
								else
								{
									$net_wins = $booster_row['wins'] - $booster_row['losses'];

									$percent = round(($net_wins / $row['number_of_wins']) * 100);
								}

								$label = "label-blue";
								$user_result = mysql_query("SELECT * FROM user WHERE id = ".$booster_row['booster']);
								$user_row = mysql_fetch_array($user_result);
								$booster = $user_row['username'];
							}
							else
							{
								$percent = 0;
								$label = "label-green";
								$booster = "No one";
							}
							$html_buffer .= '<td class=" "><div class="progress progress-info progress-striped  active" style="margin-bottom: 0px;" title="'.$percent.'%"><div style="width: '.$percent.'%;" class="bar"></div></div></td>';
							$html_buffer .= '<td class="center "><span class="label '.$label.'">'.$booster.'</span></td>';


							if($booster == $username)
							{
								$html_buffer .= '<td class="center"><a href="order.php?id='.$row['id'].'" class="btn btn-success btn-xs">Details</a><a href="unclaim.php?id='.$row['id'].'" class="btn btn-success btn-xs" onclick = "check_confirm(event)">Unclaim</a></td></tr>';

								$claimedArray[$a] = $html_buffer;
								$a++;
							}
							else if($booster != "No one")
							{
								if($type == 2 || $type == 3)
									$html_buffer .= '<td class="center"><a href="order.php?id='.$row['id'].'" class="btn btn-success btn-xs">Details</a></td></tr>';
								else
									$html_buffer .= '<td class="center"></td></tr>';

								$claimedArray[$a] = $html_buffer;
								$a++;
							}
							else
							{
								if($level >= $status_req || $type == 2 || $type == 3)
								{
									$html_buffer .= '<td class="center"><a href="claim.php?id='.$row['id'].'" class="btn btn-success btn-xs" onclick = "check_confirm(event)">Claim</a>';
								}

								if($type == 2 || $type == 3)
									$html_buffer .= '<a href="order.php?id='.$row['id'].'" class="btn btn-success btn-xs">Details</a>';

								$html_buffer .= '</td></tr>';

								$unclaimedArray[$z] = $html_buffer;
								$z++;
							}



						$html_buffer = '';
					}

					$x = 0;

					while(isset($unclaimedArray[$x]))
					{
						echo $unclaimedArray[$x];
						$x++;
					}

					$x = 0;

					//echo "<!--"; var_dump($claimedArray); echo "-->";

					while(isset($claimedArray[$x]))
					{
						echo $claimedArray[$x];
						$x++;
					}
				?>
							  </tbody></table>


      </div>
   </div>
</div>
<?php
	if(($type == 1 && $level != 0) || $type == 2 || $type == 3)
	{
		echo '<div class = "col-lg-12" style = "margin:0">
<div class = "box">
<div class="box-header">
      <div class="title">Boosters Chat</div>
   </div>
   <div class="box-content">
         <ul class="chat-box">
            <div id="chat-messages-inner">';

			$result = mysql_query("SELECT * FROM booster_chat ORDER BY timestamp DESC");

			$messages = array();

			while($row = mysql_fetch_array($result))
			{
				$subresult = mysql_query("SELECT * FROM user WHERE id = ".$row['user']);
				$subrow = mysql_fetch_array($subresult);

				$row['name'] = $subrow['username'];
				$row['email'] = $subrow['paypal'];
				$row['type'] = $subrow['type'];

				array_push($messages, $row);
			}

			$x = 0;

			while(isset($messages[$x]))
			{
				echo '<li class="arrow-box-left gray" id="msg-'.$messages[$x]['id'].'">';
				echo '<div class="avatar"><img class="avatar-small" src="images/avatars/'.$messages[$x]['type'].'.png"></div>';
				echo '<div class="info"><span class="name"><strong>'.$messages[$x]['name'].'</strong>';

				if($messages[$x]['type'] == 2)
					echo '<span class="badge badge-red">administrator</span>';
				else if($messages[$x]['type'] == 3)
					echo '<span class="badge badge-red">manager</span>';
				else
					echo '<span class="badge badge-blue">booster</span>';

				if($type == 2 || $type == 3)
					echo "<span class='badge badge-red' style = 'margin-left: 5px; cursor: pointer;' onclick = 'delete_booster_chat(".$messages[$x]['id'].")'>Remove</span>";

				echo '</span><span class="time">'.date("d M Y g:i:sA", $messages[$x]['timestamp']).'</span></div>';
				echo stripslashes($messages[$x]['message']);
				echo '</li><li class="divider"></li>';
				$x++;
			}
			echo '

</div>
         </ul>';

		 echo '

   </div>
   <div class="box-footer flat padded" style = "height: 56px">

            <div class="input-append">
               <input type="text" name="msg-box" id="msg-box" onkeyup = "check_chat_send(event)">
               <ul class="add-on" style = "margin-top: -2px; margin-right: -2px">
                  <li>
                     <button class="btn btn-success btn-xs" type="submit" onclick = "chat_send()">Send</button>
                  </li>
               </ul>
            </div>

   </div>
</div></div>';
	}
	?>
</div>
<script>
function check_chat_send (event)
{
	if(event.keyCode == 13)
	{
		if(document.getElementById("msg-box").value != "")
		{
			chat_send();
		}
	}
}
function chat_send ()
{
	if(document.getElementById("msg-box").value != "")
	{
		var ajaxHandler = new XMLHttpRequest();

		ajaxHandler.onreadystatechange = function ()
		{
			if(ajaxHandler.readyState == 4)
			{
				update_booster_chat();
				document.getElementById("msg-box").value = "";
			}
		}

		var form = new FormData();
		form.append("msg-box", document.getElementById("msg-box").value);

		ajaxHandler.open("POST", "chat-send.php");
		ajaxHandler.send(form);
	}
}
function delete_booster_chat (id)
{
	var ajaxHandler = new XMLHttpRequest();

	var form = new FormData();
	form.append("id", id);

	ajaxHandler.open("POST", "delete_booster_chat.php");
	ajaxHandler.send(form);

	var target = document.getElementById("msg-"+id);
	target.parentNode.removeChild(target);
}
function update_booster_chat ()
{
	var ajaxHandler = new XMLHttpRequest();

	ajaxHandler.onreadystatechange = function ()
	{
		if(ajaxHandler.readyState == 4)
		{
			var chat_box = document.getElementById("chat-messages-inner");

			var messages = eval( '(' + ajaxHandler.responseText + ')' );

			var i = 0;

			chat_box.innerHTML = "";
			var html_buffer = "";

			while(messages[i] != null)
			{
				html_buffer += '<li class="arrow-box-left gray" id="msg-'+messages[i]['id']+'">';
				html_buffer += '<div class="avatar"><img class="avatar-small" src="images/avatars/'+messages[i]['type']+'.png"></div>';
				html_buffer += '<div class="info"><span class="name"><strong>'+messages[i]['name']+'</strong>';

				if(messages[i]['type'] == 2)
					html_buffer += '<span class="badge badge-red">administrator</span>';
				else if(messages[i]['type'] == 3)
					html_buffer += '<span class="badge badge-red">manager</span>';
				else
					html_buffer += '<span class="badge badge-blue">booster</span>';

				<?php
					if($type == 2 || $type == 3)
						echo "html_buffer += '<span class=\"badge badge-red\" style = \"margin-left: 5px; cursor: pointer;\" onclick = \"delete_booster_chat('+messages[i]['id']+')\">Remove</span>';";
				?>

				html_buffer += '</span><span class="time">'+messages[i]['time']+'</span></div>';
				html_buffer += messages[i]['message'];
				html_buffer += '</li><li class="divider"></li>';
				i++;
			}

			chat_box.innerHTML = html_buffer;
		}
	}

	ajaxHandler.open("POST", "booster-chat-refresh.php");
	ajaxHandler.send();
}
function update_boosting_panel ()
{
	var ajaxHandler = new XMLHttpRequest();

	ajaxHandler.onreadystatechange = function ()
	{
		if(ajaxHandler.readyState == 4)
		{
			document.getElementById("boosting_inner").innerHTML = ajaxHandler.responseText;
		}
	}

	ajaxHandler.open("POST", "boosting_refresh.php");
	ajaxHandler.send();
}
function updateTimer ()
{
	update_booster_chat();
	update_boosting_panel();
	setTimeout(updateTimer, 10000);
}
window.onload = function ()
{
	<?php
		if($type != 0)
			echo 'updateTimer();';
	?>
}
function check_confirm (event)
{
	if(!confirm("Are you sure?"))
	{
		event.preventDefault();
		return false;
	}
}
</script>
</div> </div>
<?php
	include("bottom.php");
?>